//父类 其他类继承这个类
const maps = require('../common/errmap/errmap');
module.exports = class extends think.Controller {

    constructor(ctx) {
        super(ctx);
        //状态码定义
        this.successErrno = 0;
        this.successErrmsg = '成功';
        this.failErrno = -1;
        this.failErrmsg = '失败';

        //id获取
        this.id = this.get('id');

        //状态库状态定义
        this.dbStatus = {
            normal: {'status': 'normal'},
            delete: {'status': 'deleted'},
            able: {'status': 'able'},
            undisposed: {'status': 'undisposed'},
            disposed: {'status': 'disposed'},
            backed: {'status': 'backed'},
            notdeleted: {'status': ['!=', 'deleted']},
        };

        //其他数据定义
        this.dbField = {
            ipField: 'create_ip,update_ip,delete_ip',
            timeField: "create_time,delete_time,update_time"
        };
        //主键定义
        this.dbPk = {
            pain_points_solutions: "pk_pain_point_solution",
            pain_points: "pk_pain_point",
        }
    }


    /**
     * 返回成功
     * @param data
     * @param message
     * @returns {*}
     */
    resSuccess(data = '成功', message = false) {
        message !== false ? this.successErrmsg = message : '';
        return this.fail(this.successErrno, this.successErrmsg, data);
    }

    /**
     * 返回失败
     * @param data
     * @param message
     * @returns {*}
     */
    resFail(data = '失败', message = false) {
        message !== false ? this.failErrmsg = message : '';
        return this.fail(this.failErrno, this.successErrmsg, data);
    }

    resDBInsert(res) {
        if (res >= 0) {
            return this.resSuccess(res);
        }
        else {
            return this.resFail(res, "数据插入失败");
        }
    }

    resDBDelete(res) {

        if (res >= 0) {
            return this.resSuccess(res);
        }
        else {
            return this.resFail(res, "数据删除失败");
        }
    }

    resDBUpdate(res) {
        if (res >= 0) {
            return this.resSuccess(res);
        }
        else {
            return this.resFail(res, "数据更新失败");
        }
    }

    resDBSelect(res) {
        if (think.isEmpty(res)) {
            return this.resSuccess(res, "暂无数据");
        } else {
            return this.resSuccess(res, "数据获取成功");
        }
    }

    resErrmsg(errmsg = '') {
        return this.resFail('', errmsg)
    }

    resEmpty() {
        return this.resFail('数据为空', '数据为空');
    }

    notFind(){
        return this.resFail('这里什么都没有', '这里什么都没有');
    }

    /**
     * 数据验证器
     * @param data object 对象
     {
        'name':"大家好"
     {
     * @param rule object 规则
     * baseRules={
            pages: {type: "number", min: 1},
            limit: {type: "number", min: 1},
            oredername: {type: "string", enum: ['end_time','create_time','update_time']},
            orderrule: {type: "string", enum: ['desc', 'asc']},
            period: {type: "string", enum: ['past', 'now', 'forever','none']},
            range_type:{type: "string", enum: ['class', 'public']}

        };
     */
    validate(data = {}, rule = {}) {
        let vres = validate(data, baseRules);
        if (vres !== true) {
            return this.resFail(vres[0].message, vres[0].message)
        }
        return true;
    }
};